package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.CommunityComment;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommunityCommentService;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JasonResult;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/questions")
public class QusetionController {
    //注入qusetionService对象
    @Autowired
    private IQuestionService questionService;

    /**
     * 保存提问内容
     * 参数：/
     * 资源：/qusetions
     * 动作：POST
     */
    @PostMapping
    public Object save(Question question) {
        User currentUser = UserContext.getCurrentUser();
        question.setAuthor(currentUser);
        //保存提问内容
        questionService.save(question);
        return new JasonResult();
    }

    /**
     * 获取所有提问内容
     * 参数：/
     * 资源：/qusetions
     * 动作：GET
     */
    @GetMapping
    public Object listQuestions(QueryObject qo) {
        //按提问时间
        qo.setOrderBy("q.questionTime desc");
        return questionService.query(qo);
    }

    @GetMapping("/{id}")
    public Object getQuestion(@PathVariable Long id) {
        return questionService.getQuestion(id);
    }

}
